class Solution {
public:
    int maxProduct(vector<int>& nums) {
        int n = nums.size();
        vector<double> f(n + 1), g(n + 1);
        f[0] = g[0] = 1;

        double ret = INT_MIN;
        for (int i = 1; i <= n; i++)
        {
            f[i] = max(max((double)nums[i - 1], f[i - 1] * nums[i - 1]), g[i - 1] * nums[i - 1]);
            ret = max(ret, f[i]);
            g[i] = min(min((double)nums[i - 1], f[i - 1] * nums[i - 1]), g[i - 1] * nums[i - 1]);
        }
        return ret;
    }
};